<?php

/**
 * Description of G_Sqlite3
 *
 * @author kouhongfei
 */
if (class_exists('SQLite3')) {
    trigger_error('系統不支持Sqlite3');
    die();
}

class G_Sqlite3 extends GC_Db {

    protected $_sqlite3;

    public function __construct($conf) {
        if (isset($conf['path'])) {
            $this->_sqlite3 = new SQLite3($conf['path']);
        } else {
            trigger_error('数据库参数错误');
            die();
        }
    }

    public function affectedRows() {
        return $this->_sqlite3->changes();
    }

    public function begin() {
        return $this->query('begin');
    }

    public function commit() {
        return $this->query('commit');
    }

    public function escape($str) {
        return $this->_sqlite3->escapeString($str);
    }

    public function exec($sql) {
        return $this->exec($sql);
    }

    public function getLastId() {
        return $this->_sqlite3->lastInsertRowID();
    }

    public function getResults($sql) {
        $res = $this->query($sql);
        if ($res) {
            $rs = array();
            while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
                $rs[] = $row;
            }
            return $rs;
        }
        return null;
    }

    public function getRow($sql) {
        $res = $this->query($sql);
        if ($res) {
            return $res->fetchArray(SQLITE3_ASSOC);
        }
        return null;
    }

    public function getVar($sql) {
        $row = $this->getRow($sql);
        if ($res) {
            $row = $res->fetch();
            foreach ($res AS $val) {
                return $val;
            }
        }
        return null;
    }

    public function query($sql) {
        return $this->_sqlite3->query($sql);
    }

    public function rollback() {
        return $this->query('rollback');
    }

}

?>
